#include <stdio.h>
#include <stdlib.h>

#define MaxSize 5
typedef int ElemType;
typedef struct LinkNode{
    ElemType data[MaxSize]; //数组，储存MaxSize-1个元素
    int front,rear;  //队列头，队列尾
}SqQueue;

void InitQueue(SqQueue &Q)
{
    Q.front=Q.rear=0; //初始化循环队列就是让头和尾都指向0
}
//判断循环队列是否为空
bool IsEmpty(SqQueue Q)
{
    return Q.front==Q.rear;
}
//入队
bool EnQueue(SqQueue &Q,int x)
{
    if((Q.rear+1)%MaxSize==Q.front)  //判断是否队满
    {
        return false;
    }
    Q.data[Q.rear]=x;  //放入元素,尾部入队
    Q.rear=(Q.rear+1)%MaxSize;  //rear要加1，如果大于数组下表，回到开头
    return true;
}
//出队
bool DeQueue(SqQueue &Q,ElemType &x)
{
    if(Q.front==Q.rear)
    {
        return false;
    }
    x=Q.data[Q.front];  //出队列，头部出队
    Q.front=(Q.front+1)%MaxSize;
    return true;
}

//循坏队列的代码实战
int main() {
    SqQueue Q;
    InitQueue(Q);
    bool ret;
    ret=IsEmpty(Q);
    if(ret)
    {
        printf("SqQueue is empty\n");
    } else
    {
        printf("SqQueue is not empty\n");
    }
    EnQueue(Q,3);
    EnQueue(Q,4);
    EnQueue(Q,5);
    ret=EnQueue(Q,6);
    if(ret)
    {
        printf("EnQueue successfully\n");
    } else{
        printf("EnQueue failed\n");
    }
    ElemType element; //储存出队元素
    ret=DeQueue(Q,element);
    if(ret)
    {
        printf("DeQueue successfully, element is %d\n",element);
    } else{
        printf("DeQueue failed\n");
    }
    return 0;
}
